package com.ruigi.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruigi.pojo.OrderDetail;
import com.ruigi.pojo.Orders;
import com.ruigi.pojo.OrdersDto;
import com.ruigi.pojo.R;
import com.ruigi.service.OrderService;
import com.ruigi.service.OrderdetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Transactional
public class Ordercontroller {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderdetailService orderDetailService;


    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        orderService.submit(orders);

        return R.success("Success");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {
        //设置分页
        Page<Orders> objectPage = new Page<>(page, pageSize);

        Page<OrdersDto> dtoPage = new Page<>();

        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        //查询order表分页数据
        orderService.page(objectPage, ordersLambdaQueryWrapper);
        BeanUtils.copyProperties(objectPage, dtoPage, "records");
        //将order表数据转换为dto
        Page<OrdersDto> dtoPage1 = dtoPage.setRecords(objectPage.getRecords().stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            //将order表数据转换为dto
            BeanUtils.copyProperties(orders, ordersDto);
            //查询orderdetail表数据
            Long id = orders.getId();
            Orders byId = orderService.getById(id);

            String name = "用户" + byId.getUserId();
            ordersDto.setUserName(name);

            String number = byId.getNumber();

            LambdaQueryWrapper<OrderDetail> orderDetail = new LambdaQueryWrapper<>();
            orderDetail.eq(OrderDetail::getOrderId, number);
            List<OrderDetail> list = orderDetailService.list(orderDetail);
            int num = 0;
            //计算总数量
            for (OrderDetail orderDetail1 : list) {
                num += orderDetail1.getNumber();

            }
            ordersDto.setSumNum(num);
            return ordersDto;


        }).collect(Collectors.toList()));
        return R.success(dtoPage1);

    }

    //派送
    @PutMapping
    public R<String> status(@RequestBody Orders orders) {
        Long id = orders.getId();
        Orders byId = orderService.getById(id);
        byId.setStatus(orders.getStatus());
        orderService.updateById(byId);
        return R.success("Success");
    }

    //再来一单
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders) {
        Long id = orders.getId();
        Orders byId = orderService.getById(id);
        long id1 = IdWorker.getId();
        byId.setId(id1);
        byId.setOrderTime(LocalDateTime.now());
        byId.setCheckoutTime(LocalDateTime.now());
        byId.setStatus(2);
        byId.setNumber(String.valueOf(id1));
        orderService.save(byId);
        //查询orderdetail表数据
        LambdaQueryWrapper<OrderDetail> detailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        detailLambdaQueryWrapper.eq(OrderDetail::getOrderId, id);
        List<OrderDetail> list = orderDetailService.list(detailLambdaQueryWrapper);
        List<OrderDetail> collect = list.stream().map(orderDetail -> {
            long id2 = IdWorker.getId();
            orderDetail.setId(id2);
            orderDetail.setOrderId(id1);

            return orderDetail;
        }).collect(Collectors.toList());
        orderDetailService.saveBatch(collect);
        return R.success("Success");
    }


}
